package thread_pool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/23
 * Time:10:14
 * 自定义拒绝策略
 **/
public class PoolDemo16 {
    public static void main(String[] args) {
        //创建线程池
        ThreadPoolExecutor executor =  new ThreadPoolExecutor(
                5, 5, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(5),
                new RejectedExecutionHandler() {//自定义拒绝策略
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        System.out.println("执行了自定义拒绝策略");
                    }
                });


        for (int i = 0; i < 11; i++) {
            int finalI = i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("任务："+finalI+
                            "线程名称："+Thread.currentThread().getName());
                }
            });
        }

    }
     
}
